package com.pavelk.orion.view
{
	import flash.display.Sprite;
	import flash.text.*;
	import flash.net.*;
	
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import org.puremvc.as3.utilities.loadup.model.LoadupMonitorProxy;
	
	import com.pavelk.orion.ApplicationFacade;
	
	//import com.eurorscg.euro.view.components.SiteComponent;
	//import com.eurorscg.guidepost.view.events.SiteEvents;
	
	public class ApplicationMediator extends BaseMediator implements IMediator
	{
		public static const NAME:String = "ApplicationMediator";
		
		//temporary testing
		private var tf:TextField = new TextField();
		//temporary testing
				
		public function ApplicationMediator( viewComponent:Object )
		{
			super( NAME, viewComponent );
			//
			tf.x = 100;
            tf.y = 50;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.border = true;
			//root.addChild( tf );
			//			
		}
		
		override public function onRegister() : void
		{
			facade.registerMediator( new StageMediator( root ) );
			facade.registerMediator( new ProgressViewMediator( root ) );
		}
		
		override public function listNotificationInterests() : Array 
        {
            return [ 
            			LoadupMonitorProxy.LOADING_PROGRESS,
                     	LoadupMonitorProxy.LOAD_RESOURCE_TIMED_OUT,
                     	LoadupMonitorProxy.LOADING_COMPLETE,
                     	LoadupMonitorProxy.LOADING_FINISHED_INCOMPLETE,
                     	LoadupMonitorProxy.CALL_OUT_OF_SYNC_IGNORED,
                     	//LoadupMonitorProxy.LOAD_RESOURCES_REJECTED,
                     	ApplicationConstants.SITE_DATA_LOADING,
                     	ApplicationConstants.SITE_DATA_LOADED,
                     	ApplicationConstants.SITE_DATA_FAILED,
                     	ApplicationConstants.ASSETS_LOADING,
                     	ApplicationConstants.ASSETS_LOADED,
                     	ApplicationConstants.ASSETS_FAILED,
                     	ApplicationConstants.FONTS_LOADING,
                     	ApplicationConstants.FONTS_LOADED,
                     	ApplicationConstants.FONTS_FAILED                  	
                   ];
        }
        
        override public function handleNotification( note:INotification ) : void 
        {
            switch ( note.getName() ) 
            {
                /*
                case ApplicationConstants.INITIALIZE_SITE:
                	//registerChildMediators();
                	var assets:Object = note.getBody() as Object;
                	//var AssetClass:Class = assets.Background.asset;
                	var AssetClass:Class = assets.River.asset;
                	var bg:Sprite = new AssetClass();
                	root.addChild( bg );
                  	break;
                case ApplicationConstants.INITIALIZE_FONT:
                	var tf:TextField = new TextField();
                	tf.x = 100;
                	tf.y = 50;
					tf.defaultTextFormat = new TextFormat("Jazz", 36, 0x000000);
					tf.embedFonts = true;
					tf.antiAliasType = AntiAliasType.ADVANCED;
					tf.autoSize = TextFieldAutoSize.LEFT;
					tf.border = true;
					tf.text = "ZDAR";
					tf.rotation = 15;
					root.addChild(tf);
                  	break;
                 */
                 case ApplicationConstants.SITE_DATA_LOADING:
	               trace( "Loading XML…" );
	               break;
	             case ApplicationConstants.SITE_DATA_LOADED:
	               trace( "XML Loaded" );
	               break;
	             case ApplicationConstants.ASSETS_LOADING:
	               trace( "Loading Assets…" );
	               break;
	             case ApplicationConstants.ASSETS_LOADED:
	               trace( "Assets Loaded ");
	               break; 
	             case ApplicationConstants.FONTS_LOADING:
	               trace( "Loading Fonts…" );
	               break;
	             case ApplicationConstants.FONTS_LOADED:
	               trace( "Fonts Loaded" );
	               break;	               
	             case LoadupMonitorProxy.CALL_OUT_OF_SYNC_IGNORED:
	               trace( "Abnormal State, Abort" );
	               break;
	             case LoadupMonitorProxy.LOADING_PROGRESS:
	               var perc:Number = note.getBody() as Number;
	               trace( "Loading Progress: " + perc + "%" );
	               showLoadProgress( perc );
	               break;
	             case LoadupMonitorProxy.LOADING_COMPLETE:
	               trace( ">>> Loading Complete" );
	               initializeView();
	               break;
	             case LoadupMonitorProxy.LOADING_FINISHED_INCOMPLETE:
	               trace( "Loading Finished Incomplete" );
	               break;
	             case ApplicationConstants.PROGRESS_ASSETS:
	               //trace( "progress "  + note.getBody() as String );
	               break;   	  		  	
            }
        }
        
        private function showLoadProgress( p:Number ) : void
		{
			tf.text = Math.floor(p).toString();
		}
        
        private function initializeView () : void
		{
			facade.registerMediator( new RegistrationMediator( root ) );
			
			//trace("return mediator " + facade.retrieveMediator( StageMediator.NAME ));
			
			/*
			var assets:Object = note.getBody() as Object;
            //var AssetClass:Class = assets.Background.asset;
            var AssetClass:Class = assets.River.asset;
            var bg:Sprite = new AssetClass();
            root.addChild( bg );
			*/
			/*
			var tf:TextField = new TextField();
            tf.x = 100;
            tf.y = 50;
			tf.defaultTextFormat = new TextFormat("Jazz", 36, 0x000000);
			tf.embedFonts = true;
			tf.antiAliasType = AntiAliasType.ADVANCED;
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.border = true;
			tf.text = "ZDAR";
			tf.rotation = 15;
			root.addChild(tf);
			*/
		}
        
		private function registerChildMediators() : void
		{
			//var site:SiteComponent = new SiteComponent();

        	//facade.registerMediator( new SceneMediator( site.content ) );
        	//facade.registerMediator( new NavMediator( site.mainNav ) );
        	//facade.registerMediator( new SubNavMediator( site.subNav ) );
        	
        	//root.addChild( site );
        	//sendNotification( ApplicationFacade.CMD_GET_MAIN_NAV );
		}
                       
        protected function get root() : Sprite
        {
            return viewComponent as Sprite;
        }

	}
}